﻿using App_Clinique.ClassLibrary.DbContext;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace App_Clinique.ClassLibrary.Entities
{
    public partial class Patient
    {
        /****************************************************************************************************
         * Methods - Inser, Update, Delete
         ***************************************************************************************************/
        public static void Create(Patient patient)
        {
            using (App_cliniqueContext db = new App_cliniqueContext())
            {
                db.Patient.Add(patient);
                db.SaveChanges();
            }
        }

        public static void Update(Patient patient)
        {
            using (App_cliniqueContext db = new App_cliniqueContext())
            {
                db.Entry(patient).State = EntityState.Modified;
                db.SaveChanges();
            }
        }

        public static void Delete(Patient patient)
        {
            using (App_cliniqueContext db = new App_cliniqueContext())
            {
                db.Entry(patient).State = EntityState.Deleted;
                db.SaveChanges();
            }
        }

        /****************************************************************************************************
         * Methods - Select
         ***************************************************************************************************/
        public static Patient Get(Guid patientID)
        {
            Patient Obj = null;

            using (App_cliniqueContext db = new App_cliniqueContext())
            {
                Obj = (from p in db.Patient
                       where p.PatientID == patientID
                       select p).SingleOrDefault();

                Obj.Consultations.Clear();
            }

            return Obj;
        }

        public static List<Patient> GetList()
        {
            List<Patient> objs = null;

            using (App_cliniqueContext db = new App_cliniqueContext())
            {
                objs = (from p in db.Patient
                        where p.ToujoursEnVie == true
                        where p.ToujoursDansCetteClinique == true
                        select p).ToList();

                objs.ForEach(p =>
                {
                    p.Consultations.Clear();
                });
            }

            return objs;
        }
    }
}
